package ynu.elm.Dao;
/**
 * 管理员界面，点击登录后，选择管理员则，进入此界面
 * 功能如下：
 * 1.显示菜单
 * 2.所有商家列表-SQL
 * 3.搜索商家-SQL
 * 4.新建商家-SQL
 * 5.删除商家-SQL
 * 6.退出系统-exit
 */
import tools.utils;
import ynu.elm.entities.User;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Formatter;
import java.util.Scanner;

public class AdminUserDao extends User {
    AdminUserDao(User user) {
        MenuDao menu = new MenuDao();
        Scanner sc = new Scanner(System.in);
        while (true) {
            menu.showMenu("管理员");
            System.out.print("请输入选择：");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    showMcList();
                    break;
                case 2:
                    searchClient();
                    break;
                case 3:
                    addClient("yhc", "123", "hhh", 2, 2);
                    break;
                case 4:
                    System.out.println("请输入需要删除商家的id:");
                    int id = new Scanner(System.in).nextInt();
                    delClient(id);
                    break;
                case 5:
                    System.exit(0);
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }
    }
    public void showMcList(){
        try {
            //首先连接数据库
            this.getResource().setCon(utils.getConnection());
            this.getResource().setSt(this.getResource().getCon().createStatement());
            String sql="SELECT * FROM merchant";
            this.getResource().setRs(this.getResource().getSt().executeQuery(sql));
            Printf(this.getResource().getRs());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(this.getResource().getRs(),this.getResource().getSt(),this.getResource().getCon());
        }
    }
    public void searchClient(){
        try {
            int nameflag=0,addflag=0;
            this.getResource().setCon(utils.getConnection());
            String sql="SELECT * FROM merchant ";
            Scanner sc=new Scanner(System.in);
            String clName = null,clAddress=null;
            System.out.println("是否需要输入商家名称(y/n)：");
            if(sc.next().equals("y")){
                System.out.println("请输入商家名称关键词：");
                clName=new Scanner(System.in).next();
                sql=sql+"WHERE `name` LIKE ? ";
                nameflag=1;
            }

            System.out.println("是否需要输入商家地址(y/n)：");
            if(sc.next().equals("y")){
                System.out.println("请输入商家地址关键词：");
                clAddress=new Scanner(System.in).next();
                if(nameflag==1) sql=sql+"AND ";
                else sql=sql+"where ";
                sql=sql+"`address` LIKE ?";
                addflag=1;
            }

            PreparedStatement prepareStatement = this.getResource().getCon().prepareStatement(sql);
            if(nameflag==1) prepareStatement.setString(1,"%"+clName+"%");
            if(addflag==1 && nameflag==1) prepareStatement.setString(2,"%"+clAddress+"%");
            if(addflag==1 && nameflag==0) prepareStatement.setString(1,"%"+clAddress+"%");
            this.getResource().setRs(prepareStatement.executeQuery());
            Printf(this.getResource().getRs());
        } catch (
                SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(this.getResource().getRs(),this.getResource().getSt(),this.getResource().getCon());
        }
    }
    public void addClient(String name,String address,String introduce,float sendfee,float deliverycost){
        try {
            this.getResource().setCon(utils.getConnection());
            String sql="INSERT INTO merchant(`name`,`address`,`introduce`,`sendfee`,`deliverycost`) VALUES (?,?,?,?,?)";
            PreparedStatement prepareStatement1 = this.getResource().getCon().prepareStatement(sql);
            prepareStatement1.setString(1,name);
            prepareStatement1.setString(2,address);
            prepareStatement1.setString(3,introduce);
            prepareStatement1.setFloat(4,sendfee);
            prepareStatement1.setFloat(5,deliverycost);
            boolean num = prepareStatement1.execute();
            if(!num) System.out.println("插入成功");
            utils.release(this.getResource().getRs(),this.getResource().getSt(),this.getResource().getCon());
            this.getResource().setCon(utils.getConnection());
            sql="SELECT id FROM `merchant` WHERE `name`= ? ";
            PreparedStatement preparedStatement = this.getResource().getCon().prepareStatement(sql);
            preparedStatement.setString(1,name);
            this.getResource().setRs(preparedStatement.executeQuery());
            while(this.getResource().getRs().next()){
                System.out.println("商家编号为："+this.getResource().getRs().getObject("id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(this.getResource().getRs(),this.getResource().getSt(),this.getResource().getCon());
        }
    }
    public void delClient(int id){
        try {
            this.getResource().setCon(utils.getConnection());
            String sql="DELETE FROM `merchant` WHERE `id`=?";
            PreparedStatement preparedStatement = this.getResource().getCon().prepareStatement(sql);
            preparedStatement.setInt(1,id);
            boolean execute = preparedStatement.execute();
            if (!execute) System.out.println("删除成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(this.getResource().getRs(),this.getResource().getSt(),this.getResource().getCon());
        }
    }
    public void Printf(ResultSet rs) throws SQLException {
        this.getFormatter().format("%-10s\t%-25s   \t%-25s  \t%-10s \t%-5s \t%-5s","商品编号","商家名称","商家地址","商家介绍","起送费","配送费");
        System.out.println();
        while (rs.next()){
            this.getFormatter().format("%-10s\t%-25s   \t%-25s   \t%-10s  \t%-5s \t%-5s",rs.getObject("id"),rs.getObject("name"),rs.getObject("address"),rs.getObject("introduce"),rs.getObject("sendfee"),rs.getObject("deliverycost"));
            System.out.println();
        }
    }
}
